/**
 * @author yanggjun
 */

CameraController = {
		rotx: 0,
		roty: Math.PI,
		rotz: 0,
		lookAtPos:{x:100000000, y:100000000, z:-100000000,},
		cameraR : Math.sqrt(3) * 10000000,
		mousePos: {x:"", y:""},
		snipeDs: 4000,
		isSnipe: false,
		handleMouseMove: function(event){
			this.rotateCamera(event);
		},
		rotateCamera: function(event){
			this.mousePos.x = this.mousePos.x == "" ? event.clientX : this.mousePos.x;
			this.mousePos.y = this.mousePos.y == "" ? event.clientY : this.mousePos.y;
			
			var tx = this.mousePos.y - event.clientY ;
			var ty = this.mousePos.x - event.clientX ;
		//	console.log("ty:" + tx + " tx:" + ty);
			if(tx > 200 || tx < - 200) {
				this.mousePos.y = event.clientY;
//				console.log("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
				return;
			}
			if(ty > 200 || ty <- 200){
				this.mousePos.x = event.clientX;
				return;
			}
			if(this.isSnipe == true){
				this.rotx += tx/800*Math.PI;
				this.roty += ty/800*Math.PI;
			}else{
				this.rotx += tx/500*Math.PI;
				this.roty += ty/500*Math.PI;
			}
			this.mousePos.x = event.clientX;
			this.mousePos.y = event.clientY;
			this.rotate();

		},
		
		rotateCameraByKey: function(direction){
			if(direction == "l"){
				this.roty += Math.PI/100;
			}else if(direction == "r"){
				this.roty += -Math.PI/100;
			}
			this.rotate();
		},
		
		rotate: function(){
			PlayMan.direction.x =  Math.cos(this.rotx) * Math.sin(this.roty);
			PlayMan.direction.y =  Math.sin(this.rotx);
			PlayMan.direction.z =  Math.cos(this.rotx) * Math.cos(this.roty);
			
			PlayMan.rot.x = this.rotx;
			PlayMan.rot.y = this.roty;
			PlayMan.rot.z = this.rotz;
//			console.log(PlayMan.direction.x + " " + PlayMan.direction.y + " " + PlayMan.direction.z);
			
			this.lookAtPos.x = this.cameraR * PlayMan.direction.x;
			this.lookAtPos.z = this.cameraR * PlayMan.direction.z;
			this.lookAtPos.y = this.cameraR * PlayMan.direction.y;
			
			TargetIndicator.rotate();
			World.spotLight.position.set(-World.spotLightds*PlayMan.direction.x, 
					-World.spotLightds*PlayMan.direction.y,
					-World.spotLightds*PlayMan.direction.z);
			Character.updatePosition();
			if(this.isSnipe){
				this.snipe();
			}
//			console.log(this.lookAtPos.z);
			World.camera.lookAt(new THREE.Vector3(this.lookAtPos.x,this.lookAtPos.y,this.lookAtPos.z));
		},
		
		snipe: function(){
			this.isSnipe = true;
			var x = this.snipeDs * PlayMan.direction.x;
			var y = this.snipeDs * PlayMan.direction.y;
			var z = this.snipeDs *PlayMan.direction.z;
			World.camera.position.set(PlayMan.pos.x+x, PlayMan.pos.y+y, PlayMan.pos.z+z);
			MainArea.drawCrossHair(200,true);
		},
		cancelSnipe: function(){
			this.isSnipe = false;
			World.camera.position.set(PlayMan.pos.x, PlayMan.pos.y, PlayMan.pos.z);
			MainArea.drawCrossHair(200,false);
		},
}